/**
 * Created by IntelliJ IDEA.
 * User: ryan
 * Date: Sep 10, 2010
 * Time: 1:13:35 PM
 * To change this template use File | Settings | File Templates.
 */
$(function() {

    var db_name = wikid.parseDBname(window.location);
    var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"true"});

    


    var dates = wikid.findRecentChangesByDate(db, 30);
    jQuery.each(dates, function(i, dateHolder) {
        var dateDiv = $('<div class="sidedate">'+ dateHolder.date +'</div>');
        $('#recentlyChanged').append(dateDiv);
        jQuery.each(dateHolder.links, function(j, link) {
            var a = $('<div class="sidelink"><a href=' + link.href +  '>' + link.alias + '</a></div>');
            $('#recentlyChanged').append(a);
        });
    });

    $(".spaceName").text(db_name);
    document.title = db_name + " - ECKO IT";



     //var docmap = $('#docmap');
     //var result = wikid.findAllTopics(db);
    //$.each(result, function(i,topic) {
    //   //var link = $('<div class="minitopic ui-widget-content"><a href="/_'+db_name+'/topic.html?'+ doc.id +'">'+doc.id+'</a></div>') ;
    //   var link = $('<div class="minitopic ui-widget-content" title="'+ topic.id +'"><span class="ui-icon ui-icon-grip-dotted-vertical"></span><a href="/_'+db_name+'/topic.html?'+ topic.id +'">'+topic.title+'</a></div>');
    //   docmap.append(link);
//
    //});

    wikid.createTray(db, '#tray');

   //wikid.createDragToTray('.minitopic', '#tray');

    // search
    $('#search input').suggest(function(text, callback) {
        $.getJSON("/" + db_name + "/_all_docs?startkey=%22"+ text +"%22&endkey=%22"+ text +"zzz%22&limit=10", function(docs){
              var matches = [];
              for (var i = 0; i < docs.rows.length; i++) {
                if (docs.rows[i].id.indexOf(text) == 0) {
                  matches[i] = docs.rows[i].id;
                }
              }
              callback(matches);
        });
    }).keypress(function(e) {
        if (e.keyCode == 13) {
            window.location = 'topic.html?' + $(this).val();
        }
    });

});


